package com.cvr.project.activity;

import static androidx.biometric.BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE;
import static androidx.biometric.BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED;
import static androidx.biometric.BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE;
import static androidx.biometric.BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED;
import static androidx.biometric.BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED;
import static androidx.biometric.BiometricManager.BIOMETRIC_STATUS_UNKNOWN;
import static androidx.biometric.BiometricManager.BIOMETRIC_SUCCESS;
import static androidx.biometric.BiometricPrompt.ERROR_LOCKOUT;
import static androidx.biometric.BiometricPrompt.ERROR_LOCKOUT_PERMANENT;
import static androidx.biometric.BiometricPrompt.ERROR_NEGATIVE_BUTTON;
import static androidx.biometric.BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL;
import static androidx.biometric.BiometricPrompt.ERROR_NO_SPACE;
import static androidx.biometric.BiometricPrompt.ERROR_TIMEOUT;
import static androidx.biometric.BiometricPrompt.ERROR_USER_CANCELED;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.biometric.BiometricPrompt;
import androidx.lifecycle.Observer;

import com.cvr.project.R;
import com.cvr.project.api.ApiService;
import com.cvr.project.entity.IDCardEntity;
import com.cvr.project.repertory.LViewModelProviders;
import com.cvr.project.utils.BiometricUtil;
import com.cvr.project.utils.WatermarkManager;
import com.cvr.project.viewmodel.MainViewModel;
import com.gyf.immersionbar.ImmersionBar;

public class BiometricActivity extends AppCompatActivity {
    public static Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_biometric);
        context = this;
        ImmersionBar.with(this).transparentStatusBar().titleBar(R.id.app_title).navigationBarColor(R.color.white).statusBarDarkFont(true).init();
        

        
        initView();
        onPostHttpEvent();
    }

    private void onPostHttpEvent() {
        MainViewModel viewModel = LViewModelProviders.of(this, MainViewModel.class);
        viewModel.icCardLoginLiveData.observe(this, idCardEntity -> {
            WebViewActivity.getInstance(BiometricActivity.this, idCardEntity.getToken_info().getToken());
            finish();
        });
        viewModel.errorTipLiveData.observe(this, s -> Toast.makeText(BiometricActivity.this, s, Toast.LENGTH_SHORT).show());
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        TextView tvTitle = findViewById(R.id.tv_title);
        tvTitle.setText("指纹识别");
        findViewById(R.id.btn_biometric).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (BiometricUtil.getInstance().isFingerprintAvailable(BiometricActivity.this)) {
                    case BIOMETRIC_SUCCESS:
                        BiometricUtil.getInstance().authenticate(BiometricActivity.this, new BiometricPrompt.AuthenticationCallback() {

                            /**
                             * 验证过程中发生了错误
                             * @param errorCode
                             * @param errString
                             */
                            @Override
                            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                                switch (errorCode) {
                                    case ERROR_USER_CANCELED:
                                        T.showShort("取消了指纹识别");
                                        break;
                                    case ERROR_LOCKOUT:
                                        T.showShort("失败5次，已锁定，请30秒后在试");
                                        break;
                                    case ERROR_LOCKOUT_PERMANENT:
                                        T.showShort("失败次数太多，指纹验证已锁定，请改用密码方式登录");
                                    case ERROR_NEGATIVE_BUTTON:
                                        T.showShort("取消按钮");
                                        break;
                                    case ERROR_NO_DEVICE_CREDENTIAL:
                                        T.showShort("尚未设置指纹密码，请改用密码方式登录");
                                        break;
                                    case ERROR_NO_SPACE:
                                        T.showShort("可用空间不足");
                                        break;
                                    case ERROR_TIMEOUT:
                                        T.showShort("验证超时");
                                        break;
                                }
                            }
                            /**
                             * 验证成功
                             * @param
                             */
                            @Override
                            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {

                            }

                            /**
                             * 验证失败
                             * @param
                             */
                            @Override
                            public void onAuthenticationFailed() {
                                super.onAuthenticationFailed();
                                T.showShort("验证失败，请重试");
                            }
                        });

                        break;
                    case BIOMETRIC_STATUS_UNKNOWN:
                        T.showShort("未知错误，请重试");
                        break;
                    case BIOMETRIC_ERROR_UNSUPPORTED:
                        T.showShort("当前手机的Android版本不支持");
                        break;
                    case BIOMETRIC_ERROR_HW_UNAVAILABLE:
                        T.showShort("当前指纹传感器不可用，清稍后再试");
                        break;
                    case BIOMETRIC_ERROR_NONE_ENROLLED:
                        T.showShort("当前设备未录入指纹，请先录入指纹");
                        break;
                    case BIOMETRIC_ERROR_NO_HARDWARE:
                        T.showShort("未检测到手机指纹传感器");
                        break;
                    case BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED:
                        T.showShort("传感器存在已知的漏洞，在更新修复漏洞前，传感器不可用");
                        break;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        
        // 添加水印
        WatermarkManager.getInstance().addWatermark(this);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        
        // 移除水印
        WatermarkManager.getInstance().removeWatermark(this);
    }
    
    static class T {
        private static void showShort(String s) {
            Toast.makeText(BiometricActivity.context, s, Toast.LENGTH_SHORT).show();
        }
    }
}
